// pages/distribution-perfect/distribution-perfect.js
const app = getApp();
var tcity = require("../../utils/citys.js");
var api = require("../../api/api.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");
var storageManage = require("../../manage/storageManage.js");

var isEdit;
var windowHeight;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isChecked: false,
    areaSelectedStr: '请选择区域（必填）',
    areaSelectedStrType: false,
    provinces: [],
    provincesCodes: [],
    province: "",
    provinceCode: "",
    citys: [],
    citysCodes: [],
    city: "",
    cityCode: "",
    districts: [],
    districtsCodes: [],
    district: '',
    districtCode: "",
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    provinceID: '',
    detail: "",
    name: "",
    phone: "",
    addrId: "",
    showModalStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const provincesCodes = [];
    const citys = [];
    const citysCodes = [];
    const districts = [];
    const districtsCodes = [];

    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
      provincesCodes.push(cityData[i].code);
    }

    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name);
      citysCodes.push(cityData[0].sub[i].code)
    }
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      districts.push(cityData[0].sub[0].sub[i].name);
      districtsCodes.push(cityData[0].sub[0].sub[i].code);
    }

    that.setData({
      'way_index': 0,
      'provinces': provinces,
      'provincesCodes': provincesCodes,
      'citys': citys,
      'citysCodes': citysCodes,
      'districts': districts,
      'districtsCodes': districtsCodes,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'district': cityData[0].sub[0].sub[0].name,
      'provinceCode': provincesCodes[0],
      'cityCode': citysCodes[0],
      'districtCode': districtsCodes[0]

    })
  },
  /**
    * 生命周期函数--监听页面显示
    */
  onShow: function () {
    wx.getSystemInfo({
      success: function (res) {
        windowHeight = res.windowHeight;
      }
    });
  },
  //提交
  submit: utils.throttle(function () {
    var data = this.data;

    if (utils.isEmpty(data.name)) {
      toastShow("姓名为必填项");
      return;
    }

    if (data.areaSelectedStr == '请选择区域') {
      toastShow("请选择您所在城市");
      return;
    }

    if (utils.isEmpty(data.store_count)) {
      toastShow("门店数量为必填项");
      return;
    }
    if (utils.isEmpty(data.store_name)) {
      toastShow("门店名为必填项");
      return;
    }
    if (utils.isEmpty(data.detail)) {
      toastShow("详细地址为必填项");
      return;
    }
    if (utils.isEmpty(data.register_code)) {
      toastShow("邀请码为必填项");
      return;
    }

    api.default.api('register', {
      name: data.name,
      store_count: parseInt(data.store_count),
      store_name: data.store_name,
      province_code: data.provinceCode,
      city_code: data.cityCode,
      district_code: data.districtCode,
      detail: data.detail,
      register_code: data.register_code

    }, function (data) {
      if (data.e == 0) {
        var serviceUser = storageManage.getServiceUser();
        serviceUser.login = true;
        storageManage.setServiceUser(serviceUser);
        wx.reLaunch({
          url: '../loginResult/loginResult?state=true',
        })
      } else {
        toastShow(data.ed);
      }
    }, function (e) {
      console.log(e)
    });
  }),
  //事件
  nameChange: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  store_countChange: function (e) {
    this.setData({
      store_count: e.detail.value
    })
  },
  store_nameChange: function (e) {
    this.setData({
      store_name: e.detail.value
    })
  },
  detailChange: function (e) {
    this.setData({
      detail: e.detail.value
    })
  },
  register_codeChange: function (e) {
    this.setData({
      register_code: e.detail.value
    })
  },

  bindChange: function (e) {

    var val = e.detail.value
    var t = this.data.values;

    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const citysCodes = [];
      const districts = [];
      const districtsCodes = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name);
        citysCodes.push(cityData[val[0]].sub[i].code);
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        districts.push(cityData[val[0]].sub[0].sub[i].name);
        districtsCodes.push(cityData[val[0]].sub[0].sub[i].code);
      }

      this.setData({
        province: this.data.provinces[val[0]],
        provinceID: val[0],
        provinceCode: this.data.provincesCodes[val[0]],
        city: cityData[val[0]].sub[0].name,
        cityCode: cityData[val[0]].sub[0].code,
        citys: citys,
        citysCodes: citysCodes,
        district: cityData[val[0]].sub[0].sub[0].name,
        districtCode: cityData[val[0]].sub[0].sub[0].code,
        districts: districts,
        districtsCodes: districtsCodes,
        values: val,
        value: [val[0], 0, 0]
      })

      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      const districts = [];
      const districtsCodes = [];
      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        districts.push(cityData[val[0]].sub[val[1]].sub[i].name);
        districtsCodes.push(cityData[val[0]].sub[val[1]].sub[i].code);
      }

      this.setData({
        city: this.data.citys[val[1]],
        cityCode: this.data.citysCodes[val[1]],
        district: cityData[val[0]].sub[val[1]].sub[0].name,
        districtCode: cityData[val[0]].sub[val[1]].sub[0].code,
        districts: districts,
        districtsCodes: districtsCodes,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      console.log('districts no');
      this.setData({
        district: this.data.districts[val[2]],
        districtCode: this.data.districtsCodes[val[2]],
        values: val
      })
      return;
    }

    console.log(this.data)
  },
  areaSele: function () {
    console.log("areaSele");
    // this.setData({
    //   condition: !this.data.condition,
    // });
    this.showModal();
  },

  areaSele_cancel: function () {
    this.hideModal();
  },
  /**
   * button sure
   */
  areaSele_sure: function () {
    this.setData({
      condition: false,
      areaSelectedStr: this.data.province + " " + this.data.city + " " + this.data.district,
      areaSelectedStrType: true,
      isChecked: true

    });
    this.hideModal();
  },

  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 50,
      timingFunction: "linear",
      delay: 0
    })
    animation.translateY(windowHeight).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step();
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },

  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 50,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation;
    animation.translateY(windowHeight).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200);

  }
})
//提示框
function toastShow(text) {
  wx.showToast({
    title: text,
    icon: 'none',
    duration: 1000
  })
}
